package com.fengwk.cv4j.view.common;

import com.fengwk.cv4j.compute.Compute;
import com.fengwk.cv4j.factory.ViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.ViewImpl;
import com.fengwk.support.exception.BaseException;

/**
 * 通用的乘法算子
 * 
 * @author fengwk
 *
 * @param <T>
 */
public class MulView<T> extends ViewImpl<T> {

	private static final long serialVersionUID = -4007149974059343767L;

	public MulView(Compute<T> compute, ViewFactory<T> factory, @SuppressWarnings("unchecked") View<T>...vs) {
		super(compute, factory, vs);
		checkVs(2, vs);
	}
	
	@Override
	public T compute0() {
		return c().mul(t(0), t(1));
	}

	@Override
	public View<T> gradient0(int idx) {
		// y=v0*v1,y′=v0′*v1+v0*v1′
		if (idx == 0)
			return v(1);
		if (idx == 1)
			return v(0);
		throw new BaseException("idx != 0 && idx != 1");
	}
	
	@Override
	public String toString() {
		return "mul";
	}

}
